package com.kiln.juxingqs.fragment

import android.text.TextUtils
import android.view.View
import client.global.commlib.base.BaseFragment
import client.global.commlib.comm_ui.bean.Areas
import client.global.commlib.dialog.SelectAddrDialog
import client.global.commlib.dialog.SelectShengShiAddrDialog
import client.global.commlib.toast.toast
import client.global.commlib.utils.StoreUtil
import client.global.commlib.utils.listFromPath
import client.global.commlib.widget.DialogUtil
import client.global.commlib.widget.EmptyLayout
import cn.qqtheme.framework.picker.OptionPicker
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.databinding.FragmentSetxxthreeBinding
import com.kiln.juxingqs.ui.vm.SetskxxViewModel
import java.io.InputStream

class SetxxThreeFragment : BaseFragment<FragmentSetxxthreeBinding, SetskxxViewModel>() {

    var bankList: List<String>? = null

    override fun getViewId(): Int {
        return R.layout.fragment_setxxthree
    }

    override fun initViewModel(): SetskxxViewModel = createViewModel()

    override fun initWidget() {
        binding.event = this
        binding.vm = viewModel

        StoreUtil.getBean<UserInfo>(requireContext())?.let {
            bankList = it.bank_list



        }

        viewModel.callThree.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("成功")
                    requireActivity().finish()
                }
            }
        })

        binding.emptyLayout.setOnClickListener {
            viewModel.callThree()
        }

        viewModel.dataThree.observe(this, {
            it?.let {
                if (it.isSuccess) {

                    it.data?.pay_list?.let {
                        if (it.size > 0) {
                            viewModel.realNameThree.value = it.get(0).real_name
                            viewModel.nameThree.value = it.get(0).pay_name
                            viewModel.nameChildBank.value = it.get(0).bank_name
                            viewModel.payCardThree.value = it.get(0).pay_card


                            val input: InputStream = requireContext().resources.assets.open("c_areas.json")
                            val citys = input.listFromPath<Areas>()
                            var shengshis = ""
                            var shengshiIds = ""

                            it.get(0).prov_id?.let { prov_id ->
                                citys?.RECORDS?.let {
                                    for (c in it) {
                                        if (c.id.equals(prov_id)) {
                                            shengshis += c.name
                                            shengshiIds += c.id
                                        }
                                    }
                                }
                            }
                            it.get(0).area_id?.let { area_id ->
                                citys?.RECORDS?.let {
                                    for (c in it) {
                                        if (c.id.equals(area_id)) {
                                            shengshis += " ${c.name}"
                                            shengshiIds += " ${c.id}"
                                        }
                                    }
                                }
                            }
                            viewModel.shengshi.value = shengshis
                            viewModel.shengshiIds.value = shengshiIds
                        }
                    }




                    binding.emptyLayout.visibility = View.GONE
                } else {
                    toast(it.message)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
        viewModel.callThree()
    }

    fun onSelectBank() {
        bankList?.let {
            DialogUtil.showSingleDialog(
                requireActivity(),
                it.toTypedArray(),
                object : OptionPicker.OnOptionPickListener() {
                    override fun onOptionPicked(index: Int, item: String) {
                        var str = bankList!![index]
                        viewModel.nameThree.value = str

                    }
                })

        }


    }

    fun selectAddr() {
        val dialog = SelectShengShiAddrDialog(requireContext())

        var one = ""
        var two = ""
        if (!TextUtils.isEmpty(viewModel.shengshi.value)) {
            val qus = viewModel.shengshi.value!!.split(" ")
            qus?.let {
                if (qus.size >= 1)
                    one = qus[0]
                if (qus.size >= 2)
                    two = qus[1]
            }
        }
        dialog.showDialog(one, two)
        dialog.onDismis { one, two ->
            viewModel.shengshi.value = "${one.name} ${two.name}"
            viewModel.shengshiIds.value = "${one.id} ${two.id}"
        }
    }
}